package com.hd.trans.files.model;

import android.content.Context;
import android.os.AsyncTask;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.hd.trans.db.bean.MediaItem;
import com.hd.trans.files.bean.Album;
import com.hd.trans.files.bean.MediaOptions;
import com.hd.trans.loader.AlbumLoader;
import com.hd.trans.loader.MediaLoader;

import java.util.LinkedHashMap;
import java.util.List;

import static android.os.AsyncTask.Status.FINISHED;

public class MediaAlbumViewModel extends ViewModel implements DefaultLifecycleObserver {

    private MutableLiveData<List<MediaItem>> mMediaItemMutableLiveData;

    private MutableLiveData<List<Album>> mAlbumMutableLiveData;

    private MutableLiveData<LinkedHashMap<Long,MediaItem>> mSelectedMediaItemMap;

    private MediaLoader mMediaLoader;

    private AlbumLoader mAlbumLoader;

    private AsyncTask<Void, Void, List<MediaItem>> mJob;
    private AsyncTask<Void,Void,List<Album>> mAlbumJob;
    private final MediaOptions mMediaoptions;
    private Context context;

    public MediaAlbumViewModel(Context context, MediaOptions mediaOptions) {
        this.context = context;
        this.mMediaItemMutableLiveData = new MutableLiveData<>();
        this.mAlbumMutableLiveData = new MutableLiveData<>();
        this.mSelectedMediaItemMap = new MutableLiveData<>(new LinkedHashMap<>());
        this.mMediaoptions = mediaOptions;
        this.mMediaLoader = MediaLoader.getInstance(context, mMediaoptions, new Album());
        this.mAlbumLoader = AlbumLoader.getInstance(context, mMediaoptions);
    }

    public void load() {
        mJob = new AsyncTask<Void, Void, List<MediaItem>>() {

            @Override
            protected List<MediaItem> doInBackground(Void... voids) {
                try {
                    return mMediaLoader.load();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(List<MediaItem> mediaItems) {
                mMediaItemMutableLiveData.setValue(mediaItems);
            }
        }.execute((Void) null);
        mAlbumJob = new AsyncTask<Void, Void, List<Album>>() {
            @Override
            protected List<Album> doInBackground(Void... voids) {
                try {
                    return mAlbumLoader.load();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(List<Album> albums) {
                mAlbumMutableLiveData.setValue(albums);
            }
        }.execute((Void) null);
    }

    public void load(Album album) {
        this.mMediaLoader = MediaLoader.getInstance(context, mMediaoptions, album);
        if (mJob != null && mJob.getStatus() != FINISHED) {
            mJob.cancel(false);
        }
        mJob = new AsyncTask<Void, Void, List<MediaItem>>() {

            @Override
            protected List<MediaItem> doInBackground(Void... voids) {
                try {
                    return mMediaLoader.load();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(List<MediaItem> mediaItems) {
                mMediaItemMutableLiveData.setValue(mediaItems);
            }
        }.execute((Void) null);
    }

    public void updateSelectedMediaItem(LinkedHashMap<Long, MediaItem> mediaItemSet) {
        this.mSelectedMediaItemMap.setValue(mediaItemSet);
    }

    public MutableLiveData<List<MediaItem>> observeMediaList() {
        return this.mMediaItemMutableLiveData;
    }

    public MutableLiveData<LinkedHashMap<Long, MediaItem>> observeSelectedMediaItemMap() {
        return this.mSelectedMediaItemMap;
    }

    public MutableLiveData<List<Album>> getAlbumMutableLiveData() {
        return this.mAlbumMutableLiveData;
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        DefaultLifecycleObserver.super.onDestroy(owner);
        if (mJob != null && mJob.getStatus() != FINISHED) {
            mJob.cancel(false);
        }
        mJob = null;
        if (mAlbumJob != null && mAlbumJob.getStatus() != FINISHED) {
            mAlbumJob.cancel(false);
        }
        mAlbumJob = null;
    }

    public MediaOptions getMediaOptions() {
        return mMediaoptions;
    }
}
